﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FinalText
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }


        //查询
        private void Query_Click(object sender, EventArgs e)
        {
            var title = textBox1.Text;

            var sqlArt = string.Format("select *from Articles where Title like '%{0}%'", title);

            var dt = DbHelp.GetDataTable(sqlArt);

            dataGridView1.DataSource = dt;
        }
        //删除
        private void Delete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];
                var id = (int)row.Cells["Id"].Value;
                var res = MessageBox.Show("确认删除吗", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if (id > 0)
                {
                    if (res.Equals(DialogResult.Yes))
                    {
                        var sql = string.Format("delete from Articles where Id={0}", id);

                        var rowCount = DbHelp.AddOrEditOrDelectSave(sql);

                        if (rowCount > 0)
                        {
                            var title = textBox1.Text;
                            var sqlArt = string.Format("select * from Articles");

                            var dt = DbHelp.GetDataTable(sqlArt);

                            dataGridView1.DataSource = dt;

                            MessageBox.Show("删除成功");

                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("没有选择任何的行");
            }

        }
        //添加
        private void Add_Click(object sender, EventArgs e)
        {
            InitializeComponent();
            Means means = new Means();
            var res = means.ShowDialog();

            if (res == DialogResult.Yes)
            {
                var title = textBox1.Text;

                var sqlArt = string.Format("select * from Articles");

                var dt = DbHelp.GetDataTable(sqlArt);

                dataGridView1.DataSource = dt;


            }
            else
            {
                MessageBox.Show("您还未添加");

            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            var sql = "select * from Articles";
            var dataTable = DbHelp.GetDataTable(sql);
            dataGridView1.DataSource = dataTable;

            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;

        }
        //更新
        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;
                Means means = new Means(id, title, author);
                DialogResult res = means.ShowDialog();
                if (res == DialogResult.Yes)
                {
                    var title1 = textBox1.Text;
                    var sqlArt = string.Format("select * from Articles");

                    var dt = DbHelp.GetDataTable(sqlArt);

                    dataGridView1.DataSource = dt;
                    MessageBox.Show("yes");
                }

            }
            else
            {
                MessageBox.Show("未选择任何行");
            }
        }
    }
}
